﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Maticsoft.DAL;
using System.Reflection;
using System.IO;

namespace BuildingForm.PersonInfo
{
    public partial class AddStayPerson : Form
    {
        Maticsoft.DAL.Ry_Info info = new Maticsoft.DAL.Ry_Info();
        Maticsoft.DAL.Ry_Stay_Info ry_stay_info = new Maticsoft.DAL.Ry_Stay_Info();
        private string sfzh_add = string.Empty;
        public BuildingForm.StayInfoManage f;
        public AddStayPerson(BuildingForm.StayInfoManage fr)
        {
            InitializeComponent();
            Init();
            bind();
            if (fr != null)
            {
                f = fr;
            }
        }
        private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null;
        private void Init()
        {
            this.cboState2.SelectedIndexChanged -= new System.EventHandler(this.cboState2_SelectedIndexChanged);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            //表头加粗，居中
            foreach (DataGridViewColumn col in this.GridView1.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            this.GridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.GridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.GridView1.AllowUserToAddRows = false;
            this.txtName.Text = "";
            this.txtSFZH.Text = "";
            this.panel1.BorderStyle = BorderStyle.FixedSingle;
            this.panel1.Hide();

            //班组
            Maticsoft.DAL.Banz_Info info = new Banz_Info();
            DataTable dt = info.GetList("").Tables[0];
            DataRow dr3 = dt.NewRow();
            dr3["bzid"] = 0;
            dr3["bzmc"] = "全部";
            dt.Rows.InsertAt(dr3, 0);
            this.cboTeam.DisplayMember = "bzmc";
            this.cboTeam.ValueMember = "bzid";
            this.cboTeam.DataSource = dt;

            DataTable dt2 = info.GetList("").Tables[0];
            this.cboTeam2.DisplayMember = "bzmc";
            this.cboTeam2.ValueMember = "bzid";
            this.cboTeam2.DataSource = dt2;

            //状态
            Dictionary<int, string> dic = new Dictionary<int, string>();
            //dic.Add(0, "");
            dic.Add(1, "在住");
            dic.Add(2, "外住");
            BindingSource bs = new BindingSource();
            bs.DataSource = dic;
            this.cboState2.DisplayMember = "Value";
            this.cboState2.ValueMember = "Key";
            this.cboState2.DataSource = bs;
            //位置
            Maticsoft.DAL.Building buildingDao = new Maticsoft.DAL.Building();
            Maticsoft.DAL.Room roomDao = new Maticsoft.DAL.Room();
            DataTable dtBuilding = buildingDao.GetList("").Tables[0];
            DataTable dtRoom = roomDao.GetList("").Tables[0];
            this.cboAA.DisplayMember = "BuildingName";
            this.cboAA.ValueMember = "BuildingName";
            this.cboAA.DataSource = dtBuilding;
            this.cboBB.DisplayMember = "RoomName";
            this.cboBB.ValueMember = "RoomName";
            this.cboBB.DataSource = dtRoom;

            //string str = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
            //string[] arr = str.Split(' ');
            //string[] arr2 = str.Split(' ');
            //this.cboAA.DataSource = arr;
            //this.cboBB.DataSource = arr2;
            this.AutoScroll = true;
            panel1.Location = new Point((this.Size.Width - panel1.Size.Width) / 2, (this.Size.Height - panel1.Size.Height) / 2);
            this.cboState2.SelectedIndexChanged += new System.EventHandler(this.cboState2_SelectedIndexChanged);
            hide();
        }
        private void show()
        {
            if (this.m_OpaqueLayer == null)
            {
                this.m_OpaqueLayer = new MyOpaqueLayer.MyOpaqueLayer(125, false);
                this.Controls.Add(this.m_OpaqueLayer);
                this.m_OpaqueLayer.Dock = DockStyle.Fill;
                this.m_OpaqueLayer.BringToFront();
            }
            this.m_OpaqueLayer.Enabled = true;
            this.m_OpaqueLayer.Visible = true;

            //panel1在悬浮层上显示

            panel1.BackColor = Color.White;

            m_OpaqueLayer.Controls.Add(panel1);
            panel1.Show();
        }
        private void hide()
        {
            panel1.Hide();
            if (this.m_OpaqueLayer != null)
            {
                this.m_OpaqueLayer.Visible = false;
                this.m_OpaqueLayer.Enabled = false;
            }
        }
        private void bind()
        {
            string name = this.txtName.Text.ToString().Trim();
            string sfzh = this.txtSFZH.Text.ToString().Trim();
            int bz =Convert.ToInt32( this.cboTeam.SelectedValue);
            string sql = string.Empty;
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and a.xm like '%" + name + "%'";
            }
            if (!string.IsNullOrEmpty(sfzh))
            {
                sql += " and a.sfzh like '%" + sfzh + "%'";
            }
            if (bz != 0)
            {
                sql += " and b.bzid=" + bz;
            }
            sql += " order by a.createtime desc";
            DataTable dt = info.GetListByStay(sql).Tables[0];
            this.GridView1.AutoGenerateColumns = false;
            this.GridView1.DataSource = dt;
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            bind();
        }

        private void GridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            if (this.GridView1.Columns[e.ColumnIndex].Name == "colAdd")
            {
                DataGridViewRow r = GridView1.Rows[e.RowIndex];
                sfzh_add=r.Cells[2].Value.ToString();
                string xm = r.Cells[1].Value.ToString();
                string bz = r.Cells[3].Value.ToString();
                string bzid = r.Cells[9].Value.ToString();
                this.cboTeam2.SelectedIndex = GetBzidIndex(Convert.ToInt32(bzid));
                this.cboTeam2.Enabled = false;
                lblSFZH.Text = sfzh_add;
                lalName.Text = xm;
                show();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string state = this.cboState2.SelectedValue.ToString();
            string wza = this.cboAA.SelectedValue.ToString();
            string wzb = this.cboBB.SelectedValue.ToString();
            Maticsoft.Model.Ry_Info r = info.GetModel(sfzh_add);
            if (r == null)
            {
                MessageBox.Show("该人员信息已被删除！"); return;
            }
            string sql = " sfzh='" + sfzh_add + "'";
            DataTable dt =ry_stay_info.GetList(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("该人员信息已被添加！"); return;
            }
            Maticsoft.DAL.Ry_Stay_Info dao = new Ry_Stay_Info();
            Maticsoft.Model.Ry_Stay_Info obj = new Maticsoft.Model.Ry_Stay_Info();
            obj.CreateTime = DateTime.Now;
            obj.LastTime = DateTime.Now;
            obj.sfzh = sfzh_add;
            obj.state = Convert.ToInt32(state);
            obj.wza = wza;
            obj.wzb = wzb;
            obj.xm = this.lalName.Text.ToString ();
            bool isT = dao.Add(obj) > 0;
            if (isT)
            {
                MessageBox.Show("添加住宿人员成功！");
            }
            else
            {
                MessageBox.Show("添加住宿人员失败！");
            }
            hide();
            bind();
            //刷新 
            f.bind();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            hide();
        }
        private int GetCharIndex(char str)
        {
            string strs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int index = strs.IndexOf(str);
            return index;
        }

        private int GetBzidIndex(int bzid)
        {
            Maticsoft.DAL.Banz_Info info = new Banz_Info();
            DataTable dt = info.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int bzid2 = Convert.ToInt32(dt.Rows[i]["bzid"]);
                if (bzid2 == bzid)
                {
                    return i;
                }
            }
            return 0;
        }

        private void cboState2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int state = Convert.ToInt32(this.cboState2.SelectedValue);
            if (state == 1)
            {
                cboAA.Enabled = true;
                cboBB.Enabled = true;
                cboAA.SelectedIndex = 0;
                cboBB.SelectedIndex = 0;
            }
            else
            {
                cboAA.Enabled = false;
                cboBB.Enabled = false;
                cboAA.SelectedIndex = 0;
                cboBB.SelectedIndex = 0;
            }
        }
    }
}
